package com.hatic.contract;

import java.util.Map;

import org.jxstar.dao.DaoParam;
import org.jxstar.service.BusinessObject;
import org.jxstar.util.StringUtil;

import com.hatic.process.ProjectMoneyBO;

/**
 * 合同付款确认反馈金额处理。
 *
 * @author TonyTan
 * @version 1.0, 2011-11-5
 */
public class PayAuditBO extends BusinessObject {
	private static final long serialVersionUID = 1L;

	public String preAudit(String[] payIds) {
		if (payIds == null || payIds.length == 0) {
			setMessage("没有选择要确认的付款申请！");
			return _returnFaild;
		}
		
		for (int i = 0, n = payIds.length; i < n; i++) {
			if (!returnPayMoney(payIds[i])) {
				return _returnFaild;
			}
		}
		
		return _returnSuccess;
	}
	
	//反馈确认付款金额
	public boolean returnPayMoney(String payId) {
		Map<String,String> mpData = queryPay(payId);
		
		String projectId = mpData.get("project_id");
		
		double money = StringUtil.tonum(mpData.get("pay_money1"));
		
		//更新合同中的付款金额
		if (!returnContractPay(mpData)) {
			setMessage("更新合同中的付款金额出错！");
			return false;
		}
		
		//项目ID为空时，返回TRUE
		if(projectId == null || projectId.length() == 0) return true;
		
		//更新项目中的付款金额
		ProjectMoneyBO moneybo = new ProjectMoneyBO();
		if (!moneybo.addPayMoney(projectId, money)) {
			setMessage(moneybo.getMessage());
			return false;
		}
		
		return true;
	}
	
	//反馈合同中的付款金额
	public boolean returnContractPay(Map<String,String> mpData) {
		String contractId = mpData.get("contract_id");
		//double pay_money = StringUtil.tonum(mpData.get("pay_money"));
		double pay_money1 = StringUtil.tonum(mpData.get("pay_money1"));
		double contract_money = StringUtil.tonum(mpData.get("contract_money"));
		double cost_money = StringUtil.tonum(mpData.get("cost_money"));
		
		Map<String,String> mpData2 = queryContract(contractId);
		double pay_money = StringUtil.tonum(mpData2.get("pay_money"));
		
		//累计付款
		pay_money = pay_money + pay_money1;
		
		//欠款
		double last_money = 0;
		if (cost_money == 0) {
			last_money = contract_money - pay_money;
		}
		else {
			last_money = cost_money - pay_money;
		}
		if (last_money < 0) last_money = 0;
		
		//进度
		int pay_ratio = 0;
		if (cost_money == 0 ) {
			if (contract_money > 0) pay_ratio = (int) (pay_money / contract_money * 100);
		} 
		else {
			pay_ratio = (int) (pay_money / cost_money * 100);
		}
		
		String sql = "update contract set last_money = ?, pay_ratio = ?, pay_money = ? where contract_id = ?";
		
		DaoParam param = _dao.createParam(sql);
		param.addDoubleValue(Double.toString(last_money));
		param.addDoubleValue(Double.toString(pay_ratio));
		param.addDoubleValue(Double.toString(pay_money));
		param.addStringValue(contractId);
		return _dao.update(param);
	}
	
	//取付款确认金额
	private Map<String,String> queryPay(String payId) {
		String sql = "select pay_money1, pay_money, contract_money, cost_money, contract_id, project_id from contract_pay where pay_id = ?";
		DaoParam param = _dao.createParam(sql);
		param.addStringValue(payId);
		return _dao.queryMap(param);
	}
	//取付款确认金额
	private Map<String,String> queryContract(String contractId) {
		String sql = "select contract_id,pay_money from contract where contract_id = ?";
		DaoParam param = _dao.createParam(sql);
		param.addStringValue(contractId);
		return _dao.queryMap(param);
	}
}
